package com.open.yujiakj.common.utils.redis;

import com.open.yujiakj.common.exception.BizException;
import com.open.yujiakj.common.utils.bean.SerializeUtil;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author honglei
 * @version 1.0
 * @description: TODO
 * @date 2025/5/14 10:51
 */
@Slf4j
public class RedisUtil {
    private static JedisPool pool = null;
    private static JedisCluster jedisCluster;


    private void checkRedis() {
        if (null == jedisCluster && null == pool) {
            throw new BizException(50001, "请先配置redis");
        }
    }


    public void set(String key, Object value) {
        checkRedis();
        if (null != jedisCluster) {
            jedisCluster.set(key.getBytes(), SerializeUtil.serialize(value));
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            jedis.set(key.getBytes(), SerializeUtil.serialize(value));
            jedis.close();
        }
    }

    public void set(String key, Object value, int second) {
        checkRedis();
        if (null != jedisCluster) {
            jedisCluster.set(key.getBytes(), SerializeUtil.serialize(value));
            jedisCluster.expire(key.getBytes(), second);
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            jedis.set(key.getBytes(), SerializeUtil.serialize(value));
            jedis.expire(key.getBytes(), second);
            jedis.close();
        }
    }

    public Object get(String key) {
        checkRedis();
        byte[] data = null;
        if (null != jedisCluster) {
            data = jedisCluster.get(key.getBytes());
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            data = jedis.get(key.getBytes());
            jedis.close();
        }
        if (data == null || data.length <= 0) {
            return null;
        }
        return SerializeUtil.unserialize(data);
    }

    public void del(String key) {
        checkRedis();
        if (null != jedisCluster) {
            jedisCluster.del(key.getBytes());
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            jedis.del(key.getBytes());
            jedis.close();
        }
    }

    public void hset(String key, String field, Object value) {
        checkRedis();
        if (null != jedisCluster) {
            jedisCluster.hset(key.getBytes(), field.getBytes(), SerializeUtil.serialize(value));
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            jedis.hset(key.getBytes(), field.getBytes(), SerializeUtil.serialize(value));
            jedis.close();
        }
    }

    public Object hget(String key, String field) {
        checkRedis();
        byte[] data = null;
        if (null != jedisCluster) {
            data = jedisCluster.hget(key.getBytes(), field.getBytes());
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            data = jedis.hget(key.getBytes(), field.getBytes());
            jedis.close();
        }
        if (data == null || data.length <= 0) {
            return null;
        }
        return SerializeUtil.unserialize(data);
    }

    public List<Object> hgetAll(String key) {
        checkRedis();
        List list = new ArrayList();
        Map map = new HashMap();
        if (null != jedisCluster) {
            map = jedisCluster.hgetAll(key.getBytes());
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            map = jedis.hgetAll(key.getBytes());
            jedis.close();
        }
        if (map != null && map.size() > 0) {
            for (Object object : map.keySet()) {
                byte[] data = (byte[]) map.get(object);
                if (data != null && data.length > 0) {
                    list.add(SerializeUtil.unserialize(data));
                }
            }
        }
        return list;
    }


    public void hdel(String key, String field) {
        checkRedis();
        if (null != jedisCluster) {
            jedisCluster.hdel(key.getBytes(), field.getBytes());
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            jedis.hdel(key.getBytes(), field.getBytes());
            jedis.close();
        }
    }


    public long getAutoId(String key) {
        checkRedis();
        long id = 1;
        if (null != jedisCluster) {
            id = jedisCluster.incr(key.getBytes());
        }
        if (null != pool) {
            Jedis jedis = pool.getResource();
            id = jedis.incr(key.getBytes());
            jedis.close();
        }
        return id;
    }

    public void lpush(String key, Object value) {
        checkRedis();
        if (null!=jedisCluster){
            jedisCluster.lpush(key.getBytes(), SerializeUtil.serialize(value));
        }
        if (null!=pool){
            Jedis jedis = pool.getResource();
            jedis.lpush(key.getBytes(), SerializeUtil.serialize(value));
            jedis.close();
        }
    }

    public void rpush(String key, Object value) {
        checkRedis();
        if (null!=jedisCluster){
            jedisCluster.rpush(key.getBytes(), SerializeUtil.serialize(value));
        }
        if (null!=pool){
            Jedis jedis = pool.getResource();
            jedis.rpush(key.getBytes(), SerializeUtil.serialize(value));
            jedis.close();
        }

    }

    public List<Object> lrange(String key) {
        checkRedis();
        List<byte[]> list=null;
        if (null!=jedisCluster){
            list = jedisCluster.lrange(key.getBytes(), 0, -1);
        }
        if (null!=pool){
            Jedis jedis = pool.getResource();
            list = jedis.lrange(key.getBytes(), 0, -1);
            jedis.close();
        }
        List<Object> result = new ArrayList<Object>(list.size());
        return result;
    }

    public Object lpop(String key) {
        checkRedis();
        byte[] bs=null;
        if (null != jedisCluster){
            bs = jedisCluster.lpop(key.getBytes());
        }
        if (null != pool){
            Jedis jedis = pool.getResource();
            bs = jedis.lpop(key.getBytes());
            jedis.close();

        }
        if (bs == null || bs.length <= 0) {
            return null;
        }
        Object object = SerializeUtil.unserialize(bs);
        return object;
    }

    public Object rpop(String key) {
        checkRedis();
        byte[] bs=null;
        if (null != jedisCluster){
            bs = jedisCluster.rpop(key.getBytes());
        }
        if (null != pool){
            Jedis jedis = pool.getResource();
            bs = jedis.rpop(key.getBytes());
            jedis.close();

        }
        if (bs == null || bs.length <= 0) {
            return null;
        }
        Object object = SerializeUtil.unserialize(bs);
        return object;
    }

    public Object lindex(String key, int index) {
        checkRedis();
        byte[] bs=null;
        if (null != jedisCluster){
            bs = jedisCluster.lindex(key.getBytes(), index);
        }
        if (null != pool){
            Jedis jedis = pool.getResource();
            bs = jedis.lindex(key.getBytes(), index);
            jedis.close();

        }
        if (bs == null || bs.length <= 0) {
            return null;
        }
        Object object = SerializeUtil.unserialize(bs);
        return object;

    }

}
